﻿using System;
using System.Linq;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;

namespace Wala.Connection.Encrypts
{
    public class Encrypt
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns>encrypt MD5 Algorithm string</returns>
        public static string EncryptMD5(string str)
        {
            string rethash = "";
            try
            {
                HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);                
                var hash = alg.CreateHash();
                IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
                hash.Append(buffMsg1);
                IBuffer buffHash1 = hash.GetValueAndReset();
                string strHash1 = CryptographicBuffer.EncodeToBase64String(buffHash1);
                return strHash1;
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;            
        }

        public static string EncryptSha1(string str)
        {
            string rethash = "";
            try
            {
                HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
                var hash = alg.CreateHash();
                IBuffer buffMsg1 = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
                hash.Append(buffMsg1);
                IBuffer buffHash1 = hash.GetValueAndReset();
                string strHash1 = CryptographicBuffer.EncodeToBase64String(buffHash1);
                return strHash1;
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;
        }
    }
}
